import re
import time

from common.BasePages import BasePage


class UserPage(BasePage):
    def user_url(self):
        self.goto_url("systemManagement/user")

    def create_user(self):
        self.page.get_by_role("button", name="新增").click()
        self.page.get_by_role("dialog", name="用户新增").get_by_placeholder("请输入姓名").click()
        self.page.get_by_role("dialog", name="用户新增").get_by_placeholder("请输入姓名").fill("注册用户1")
        self.page.get_by_role("textbox", name="请输入联系电话").click()
        self.page.get_by_role("textbox", name="请输入联系电话").fill("13131085533")
        self.page.get_by_role("textbox", name="请输入账号").click()
        self.page.get_by_role("textbox", name="请输入账号").fill("12345678")
        self.page.get_by_role("textbox", name="请输入登录密码").click()
        self.page.get_by_role("textbox", name="请输入登录密码").fill("12345678")
        self.page.get_by_role("textbox", name="请再一次输入密码").click()
        self.page.get_by_role("textbox", name="请再一次输入密码").fill("12345678")
        self.page.get_by_label("用户新增").locator("form div").filter(
            has_text="登录账号 8/16 有效期至状态 无数据 多端登录").get_by_role("textbox").nth(1).click()
        self.page.locator(".available > div > span").first.click()
        self.page.locator(
            "div:nth-child(3) > .el-date-table > tbody > tr:nth-child(5) > td:nth-child(7) > div > span").click()
        self.page.get_by_role("button", name="确定").click()
        self.page.get_by_role("button", name="确 定").click()
        self.assert_alert_content("新增成功")

    def edit_user(self):
        self.page.get_by_role("row", name="  ", exact=True).locator("i").nth(1).click()
        self.page.get_by_role("textbox", name="请输入工号").click()
        self.page.get_by_role("textbox", name="请输入工号").fill("222")
        self.page.get_by_role("button", name="确 定").click()
        assert self.page.get_by_text("编辑成功")

    def delete_user(self):
        self.page.locator(
            ".el-table__fixed-body-wrapper > .el-table__virtual-wrapper > div > .el-table__body > tbody > tr > .el-table_1_column_3 > .cell > .basicTableBtnBox > .el-icon-delete").first.click()
        self.page.get_by_role("button", name="确定").click()
        self.assert_alert_content("删除成功!")

    def user_login(self):
        login_data = self.read_yaml_json("configs/config.yaml", "$..formal_login")
        self.login(login_data['url'], "12345678", "12345678")
        self.page.get_by_role("textbox", name="请输入问题").first.click()
        self.page.get_by_role("textbox", name="请输入问题").first.fill("1")
        self.page.get_by_role("textbox", name="请输入答案一").click()
        self.page.get_by_role("textbox", name="请输入答案一").fill("1")
        self.page.get_by_role("textbox", name="请输入问题").nth(1).click()
        self.page.get_by_role("textbox", name="请输入问题").nth(1).fill("2")
        self.page.locator(
            "div:nth-child(4) > .el-col > .el-form-item > .el-form-item__content > .el-input > .el-input__inner").click()
        self.page.locator(
            "div:nth-child(4) > .el-col > .el-form-item > .el-form-item__content > .el-input > .el-input__inner").fill(
            "2")
        self.page.get_by_role("textbox", name="请输入问题").nth(2).click()
        self.page.get_by_role("textbox", name="请输入问题").nth(2).fill("3")
        self.page.locator(
            "div:nth-child(6) > .el-col > .el-form-item > .el-form-item__content > .el-input > .el-input__inner").click()
        self.page.locator(
            "div:nth-child(6) > .el-col > .el-form-item > .el-form-item__content > .el-input > .el-input__inner").fill(
            "3")
        response = self.cat_url(self.page.get_by_role("button", name="确 定"), "Project")
        print(response)
        # with self.page.expect_response(f"**/Project/**") as response_info:
        #     self.login(login_data['url'], "12345678", "12345678")
        #     response = response_info.value  # 获取接口响应的结果。
        #     return response.json()



